class: center, middle, inverse, title-slide # {ggplot2} Essentials --- layout: true <div class="dk-footer"> <span> <a href="https://rfortherestofus.com/" target="_blank">R for the Rest of Us </a> </span> </div> --- class: center, middle, dk-section-title background-image:url("images/pexels-photo-5282585.jpeg") background-size: cover # {ggplot2} essentials --- ### What is `{ggplot2}`? `{ggplot2}` implements a *grammar of graphics* for building charts. We can use it to consistently build **any**<sup>1</sup> chart. <!-- --> .footnote[ [1] - Dual y-axis charts must be transformations of one another ([for good reasons](https://stackoverflow.com/a/3101876/1659890)) ] --- # Building blocks of a {ggplot2} chart -
<span style='color:#c03728'>Aesthetics</span> -
<span style='color:#919c4c'>Geoms</span> -
<span style='color:#919c4c'>Scales</span> -
<span style='color:#919c4c'>Guides</span> -
<span style='color:#e68c7c'>Theme</span> --- # Building blocks of a {ggplot2} chart -
<span style='color:#c03728'>Aesthetics</span>: these create mappings between columns in datasets and the coordinate system of the chart. --- # Building blocks of a {ggplot2} chart -
<span style='color:#c03728'>Aesthetics</span> -
<span style='color:#919c4c'>Geoms</span>: these use the aesthetics to draw layers onto our charts --- # Geoms & categorical variables In R we use `factors` to control the order in which geoms are drawn (and appear in legends) .pull-left[ - Default (alphabetical) ordering <img src="data:image/png;base64,#02_01_ggplot2-essentials_files/figure-html/unnamed-chunk-4-1.png" width="100%" /> ] .pull-right[ - Reordered by another column <img src="data:image/png;base64,#02_01_ggplot2-essentials_files/figure-html/unnamed-chunk-5-1.png" width="100%" /> ] --- # Building blocks of a {ggplot2} chart -
<span style='color:#c03728'>Aesthetics</span> -
<span style='color:#919c4c'>Geoms</span> -
<span style='color:#919c4c'>Scales</span>: these affect how the aesthetics appear in the chart --- # Building blocks of a {ggplot2} chart -
<span style='color:#c03728'>Aesthetics</span> -
<span style='color:#919c4c'>Geoms</span> -
<span style='color:#919c4c'>Scales</span> -
<span style='color:#919c4c'>Guides</span>: these affect how the guides (or legends) are constructed from the aesthetics and scales. --- # Guides and maps <center> <img src="data:image/png;base64,#02_01_ggplot2-essentials_files/figure-html/unnamed-chunk-6-1.png" width="900px" /> </center> --- # Building blocks of a {ggplot2} chart -
<span style='color:#c03728'>Aesthetics</span> -
<span style='color:#919c4c'>Geoms</span> -
<span style='color:#fd8f24'>Scales</span> -
<span style='color:#f5c04a'>Guides</span> -
<span style='color:#e68c7c'>Theme</span>: this affects the look and feel of the chart. --- # {ggspatial} The `{ggspatial}` package extends `{ggplot2}`'s mapping capabilities in three big ways: - It allows us to add map tiles to maps -- <center> <img src="data:image/png;base64,#02_01_ggplot2-essentials_files/figure-html/unnamed-chunk-7-1.png" width="65%" /> ``` ## | | | 0% | |== | 3% | |===== | 7% | |======= | 10% | |========= | 13% | |============ | 17% | |============== | 20% | |================ | 23% | |=================== | 27% | |===================== | 30% | |======================= | 33% | |========================== | 37% | |============================ | 40% | |============================== | 43% | |================================= | 47% | |=================================== | 50% | |===================================== | 53% | |======================================== | 57% | |========================================== | 60% | |============================================ | 63% | |=============================================== | 67% | |================================================= | 70% | |=================================================== | 73% | |====================================================== | 77% | |======================================================== | 80% | |========================================================== | 83% | |============================================================= | 87% | |=============================================================== | 90% | |================================================================= | 93% | |==================================================================== | 97% | |======================================================================| 100% ``` </center> --- # {ggspatial} The `{ggspatial}` package extends `{ggplot2}`'s mapping capabilities in three big ways: - It allows us to add map tiles to maps - It allows us to add `{raster}` and `{terra}` datasets to `{ggplot2}` charts -- - It allows us to add map-specific chart furniture to `{ggplot2}` charts <center> <img src="data:image/png;base64,#02_01_ggplot2-essentials_files/figure-html/unnamed-chunk-8-1.png" width="250px" /> </center>